package com.onlyyou.pictureserver.service.Impl;

import com.onlyyou.pictureserver.service.PictureUpload;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author: 小野
 * @Date: 2020/1/16 18:09
 */
@Service
public class PictureUploadImpl implements PictureUpload {
    @Override
    public String upload() throws IOException, MyException {
        //1、把FastDFS提供的jar包添加到工程中
        //2、初始化全局配置。加载一个配置文件。
        ClientGlobal.init("D:/fdfs.conf");
        //3、创建一个TrackerClient对象。
        TrackerClient trackerClient = new TrackerClient();

        //4、创建一个TrackerServer对象。
        TrackerServer trackerServer = trackerClient.getConnection();
        //5、声明一个StorageServer对象，null。
        StorageServer storageServer = null;
        //6、获得StorageClient对象。
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);

        //7、直接调用StorageClient对象方法上传文件即可。
        String[] result = storageClient.upload_file("D:/logo.png", "png", null);
        StringBuilder sb = new StringBuilder("http://176.198.106.58/");
        sb.append(result[0]).append("/").append(result[1]);
        System.out.println("图片访问地址: " + sb.toString());
        return null;
    }
}
